package com.tenglong.teacher.ui.common.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.cuotibao.zhuoxiao.basics.utils.DensityUtil;
import com.tenglong.teacher.R;

public class SelecSlideView extends PopupWindow {
    private Context mContext;
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private RecyclerView.Adapter adapter;
    private float with = ViewGroup.LayoutParams.MATCH_PARENT;
    private float height = ViewGroup.LayoutParams.MATCH_PARENT;

    public SelecSlideView(Context context, RecyclerView.LayoutManager layoutManager, RecyclerView.Adapter adapter) {
        super(context);
        this.mContext = context;
        this.adapter = adapter;
        this.layoutManager = layoutManager;
        init();
    }

    public SelecSlideView(Context context, float with, float height, RecyclerView.LayoutManager layoutManager, RecyclerView.Adapter adapter) {
        super(context);
        this.mContext = context;
        this.adapter = adapter;
        this.layoutManager = layoutManager;
        this.with = with;
        this.height = height;
        init();
    }


    @Override
    public void showAsDropDown(View anchor) {
        handleVersion(anchor);
        super.showAsDropDown(anchor);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        handleVersion(anchor);
        super.showAsDropDown(anchor, xoff, yoff);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) {
        handleVersion(anchor);
        super.showAsDropDown(anchor, xoff, yoff, gravity);
    }

    private void handleVersion(View anchor) {
        if (Build.VERSION.SDK_INT >= 24) {
            Rect visibleFrame = new Rect();
            anchor.getGlobalVisibleRect(visibleFrame);
            int height = anchor.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
            setHeight(height);
        }
    }

    private void init() {
        setHeight((int) height);
        setWidth((int) with);
        setFocusable(true);
        setAnimationStyle(R.style.Widget_AppCompat_ListPopupWindow);
        setBackgroundDrawable(new ColorDrawable(Color.parseColor("#66000000")));
        initView();
    }

    private void initView() {
        ViewGroup rootView = (ViewGroup) LayoutInflater.from(mContext).inflate(R.layout.relate_answer_poppw_layout, null);
        rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isShowing()) {
                    dismiss();
                }
            }
        });
        recyclerView = rootView.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                Context context = parent.getContext();
                int index = parent.getChildAdapterPosition(view);
                int t = DensityUtil.dp2px(15);
                outRect.bottom = t;
                if ((index + 1) % 3 == 0) {
                    outRect.left = t / 2;
                    outRect.right = t;
                } else if ((index + 1) % 3 == 1) {
                    outRect.left = t;
                    outRect.right = t / 2;
                } else if ((index + 1) % 3 == 2) {
                    outRect.left = t / 2;
                    outRect.right = t / 2;
                }
                if (index < 3) {
                    outRect.top = DensityUtil.dp2px((float) 16.25);
                }
            }
        });
        recyclerView.setAdapter(adapter);
        setContentView(rootView);
    }
}
